<?php

class Admin_ActivitesController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        // action body
        $mod = new Application_Model_ActivityModel();


        $this->view->nb = $mod->countAll();
        $this->view->tabActivity = $mod->readAll();
        $this->view->comments = $mod->getComments();
    }

    public function addAction() {
        $mod = new Application_Model_ActivityModel();
        $form = new Admin_Form_Activity();
        $this->view->form = $form;

        // si le formulaire a été soumis
        if ($this->_request->isPost()) {
// on récupère les variables Post
            $formData = $this->_request->getPost();
// si les critères de validation sont respectés
            if ($form->isValid($formData)) {
                $id = $form->getValue('firstname');
                $owner_firstname = $form->getValue('owner_firstname');
                $owner_name = $form->getValue('owner_name');
                $content = $form->getValue('event_content');
                $title = $form->getValue('title');


                $activity = new Application_Model_Activity();
                $activity->setId($id);
                $owner = new Application_Model_People($owner_name, $owner_firstname);
                $activity->setOwner($owner);
                $activity->setContent($content);
                $activity->setTitle($title);

                if ($form->image->receive()) {
                    $fileName = $form->image->getFileName();
                    $fileName = substr($fileName, strripos($fileName, "/public/images"));
                    $activity->setPicturepath($fileName);
                }
                $mod->insert($activity);
//on nettoie les champs du formulaire .
                $form->reset();
            }
        }
    }

    public function readallAction() {
        // action body
    }

    public function editAction() {
        // action body
        $mod = new Application_Model_ActivityModel();
        $id = $this->getRequest()->getParam('id');
        $activity = $mod->read($id);
        $this->view->activity = $activity;
        $form = new Admin_Form_Activity();
        $form->setId($activity->getId());
        $form->init();
        $this->view->form = $form;

        // si le formulaire a été soumis
        if ($this->_request->isPost()) {
// on récupère les variables Post
            $formData = $this->_request->getPost();
// si les critères de validation sont respectés
            if ($form->isValid($formData)) {
                $id = $form->getValue('id');
                $owner_firstname = $form->getValue('owner_firstname');
                $owner_name = $form->getValue('owner_name');
                $content = $form->getValue('event_content');
                $title = $form->getValue('title');

                $activity = new Application_Model_Activity();
                $activity->setId($id);
                $owner = new Application_Model_People($owner_name, $owner_firstname);
                $activity->setOwner($owner);
                $activity->setContent($content);
                $activity->setTitle($title);

                if ($form->image->receive()) {
                    $fileName = $form->image->getFileName();
                    $fileName = substr($fileName, strripos($fileName, "/public/images"));
                    $activity->setPicturepath($fileName);
                }
                $mod->edit($id, $activity);
//on nettoie les champs du formulaire .
                //$form->reset();
            }
        }
    }

    public function deleteAction() {
        $mod = new Application_Model_ActivityModel();
        $id = $this->getRequest()->getParam('id');
        $mod->delete($id);
        // action body
    }

    public function deletecommentAction() {
        $mod = new Application_Model_ActivityModel();
        $id = $this->getRequest()->getParam('id');
        $mod->removeComment($id);
        $this->_forward('index');
        // action body
    }

}

